home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 8: LINUX Games
/
Linux Cubed Series 8 - LINUX Games.iso
/
games
/
video
/
trojka.tgz
/
trojka.tar
/
trojka
/
hiscore.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-25
|
2KB
|
128 lines
/*
* @(#) hiscore.c -- hiscore system for TROJKA
* created: 4.iii.1992
* modified: 13.iii.1992
*/
#include "sys_custom.h"
#include "hiscore.h"
#include "trojka.h"
#include <stdio.h>
#include <fcntl.h>
int gameover(),
shiftscores(),
readscores(),
writescores(),
scorelist();
extern struct scorefile *sfile;
extern unsigned long
score,
wipes,
blocks; /* trojka */
extern int
speed;
extern int position;
int gameover()
{
struct scorefile *i, *a, *eof;
showscore();
showgameover();
eof = &sfile[NUMSCORES];
position = NUMSCORES+1; /* initially no position in hiscore table */
a = &sfile[0];
while (a < eof) {
if (score > a->points) {
position = (a-sfile)+1;
break;
}
a++;
}
/* open up score list */
eof--;
i = eof;
while(eof > a)
*(i--) = *(--eof);
if (position < NUMSCORES) { /* YOU ARE IN THE LIST */
a->points = score;
a->stage = speed;
while(getname(a->name)==0) /* empty name not allowed */
;
writescores(); /* write scores to disk */
}
}
int readscores()
{
int fd, old_umask;
struct scorefile *eof, *i;
eof = &sfile[NUMSCORES];
for(i = sfile; i < eof; i++) { /* Assume default scores */
strcpy(i->name, STDNAME);
i->points = 333;
i->stage = STDLEVEL;
}
fd = open(SCOREFILE, O_RDONLY);
if(fd == -1) { /* if there's no highscore file -> create one */
old_umask = umask(0);
fd = creat(SCOREFILE, UMASK);
umask(old_umask);
if(fd == -1)
quit_prog("Error creating scorefile");
close(fd);
writescores(); /* write the default scores */
if((fd = open(SCOREFILE, O_RDWR)) == -1)
quit_prog("Error opening scorefile");
}
if(read(fd,sfile, FILE_SIZE) == -1)
quit_prog("Error reading scorefile");
close(fd);
}
int writescores()
{
int fd;
sfile = &sfile[0];
if((fd = open(SCOREFILE, O_WRONLY)) == -1)
quit_prog("Error opening scorefile.");
if((write(fd, sfile, FILE_SIZE)) == -1)
quit_prog("Score-file write error.");
if((close(fd)) == -1)
quit_prog("Error closing scorefile.");
}
int scorelist()
{ /* used for the -s option */
struct scorefile *eof, *i;
readscores();
eof = &sfile[NUMSCORES-1];
fprintf(stderr,"\nBest Trojka-Czars\n-----------------\n\n");
for(i=sfile; i<eof; i++)
fprintf(stderr,"%-22s %7lu %3d\n",
i->name,
i->points,
i->stage
);
fprintf(stderr,"\n\n");
}